import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo;

/**
 * @Author @fiber-cloud
 * 手写List
 * @Date 2025/7/27 10:50
 */
public class MySingleList {

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;


    //返回整个链表的长度
    public int size(){
        if (this.head == null){
            return 0;
        }
        ListNode cur = this.head;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //打印链表中的数据，默认从头开始
    public void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定节点newHead开始打印链表
     */
    public void display(ListNode cur){
        ListNode cur1 = cur;
        while (cur1 != null){
            System.out.print(cur1.val+" ");
            cur1 = cur1.next;
        }
        System.out.println();
    }
    //逆序打印
    public void display2(ListNode cur){
        if (cur == null){
            return;
        }
        if (head.next == null){
            System.out.print(head.val + " ");
            return;
        }
        display2(head.next);
        System.out.print(head.val + " ");
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int val){
        ListNode cur = this.head;
        while (cur != null){
            if(cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        cur.next = head;
        head = cur;
    }
    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        ListNode temp = this.head;
        if (temp == null){
            this.head = cur;
        }else {
            while (temp.next != null){
                temp = temp.next;
            }
            temp.next = cur;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws IndexWrongFulException {
        //先检查输入的数据是否合法
        if (index < 0 || index > size()){
            System.out.println("index位置不合法");
            throw new IndexWrongFulException("index位置不合法");
        }

        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }

        ListNode cur = findIndexSubOne(index);
        ListNode node = new ListNode(data);

        node.next = cur.next;
        cur.next = node;

    }

    //走index-1步
    private ListNode findIndexSubOne(int index) {
        ListNode cur = this.head;
        while (index-1 != 0){
            cur = cur.next;
            index-- ;
        }
        return cur;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //判断链表是否为空
        if (this.head == null){
            return;
        }
        if (this.head.val == key){
            this.head = this.head.next;
            return;
        }

        ListNode cur = findPrevOfKey(key);
        if (cur == null){
            System.out.println("没有你要删除的数据");
            return;
        }
        ListNode temp = cur.next;
        cur.next = temp.next;
    }
    //找到key的前驱节点
    private ListNode findPrevOfKey(int key) {
        ListNode cur = this.head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }


    //删除整个链表中所有值为key的节点
    public void removeAllKey(int key){
        //判断链表是否为空
        if (this.head == null){
            return;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;

        while (cur != null){
            if (cur.val ==  key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key){
            this.head = this.head.next;
        }
    }

    //倒数第K个节点
    public ListNode FindKthToTail(int k){
        if (k <= 0|| k >size()){
            return null;
        }

        //快慢指针
        ListNode slow = this.head;
        ListNode fast = this.head;

        //fast先走k-1步
        while (k-1 != 0){
            fast = fast.next;
            if (fast == null){
                return null;
            }
            k--;
        }

        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }



    //链表分割
    public ListNode partition(ListNode pHead, int x) {
        ListNode bs = null;
        ListNode be = null;

        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;
        //遍历链表
        while (cur != null){
           if (cur.val < x){
               //第一次进行插入节点
               if (bs == null ){
                   bs = cur;
                   be = cur;
               }else {
                   be.next = cur;
                   be = be.next;
               }
           }else {
               if (as == null){
                   as = cur;
                   ae = cur;
               }else {
                   ae.next = cur;
                   ae = ae.next;
               }
           }
           cur = cur.next;
        }

        //第一个段里面没有数据
        if (bs == null){
            return as;
        }
        be.next = as ;

        if (as != null){
            ae.next = null;
        }
        return bs;
    }


    //链表的回文结构
    public boolean chkPalindrome(ListNode head) {
       if (head == null){
          return false;
       }
       if (head.next == null){
           return true;
       }


        ListNode curNext = null;
        //1.找到链表的中间节点
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }

        // 2.翻转中间节点后面的链表
        ListNode cur = slow.next;
        while (cur != null){
            curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        // 3.一个从头开始 一个从尾部开始
        cur = head;
        while (head != slow){
            if (head.val != slow.val){
                return false;
            }
            if (head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }


    //相交链表
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //1.求链表长度
        ListNode pl = headA;//永远指向最长的链表
        ListNode ps = headB;//永远指向最短的链表

        int lenA = 0;
        while (pl != null){
            lenA++;
            pl = pl.next;
        }
        int lenB = 0;
        while (ps != null){
            lenB++;
            ps = ps.next;
        }


        pl = headA;
        ps = headB;

        int len = lenA - lenB;
        if (len < 0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }

        //2. 让最长的链表 先走len步
        while (len != 0){
            pl = pl.next;
            len--;
        }
        // 3.pl和ps 现在在相同的起始位置
        while (pl != ps){
            pl = pl.next;
            ps = ps.next;
        }
        // 走到这里，pl和ps相等了，pl == ps == null
        if (pl == null){
            return null;
        }
        return pl;
    }

    //环形链表
    public boolean hasCycle(ListNode head) {
        //追及相遇问题
        if (head == null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                return true;
            }
        }
        return false;
    }

    //环形链表Ⅱ
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                ListNode temp = head;
                while (temp != slow){
                    temp = temp.next;
                    slow = slow.next;
                }
                return temp;
            }
        }
        return null;
    }


}
